package elements
{
	import flash.media.VideoCodec;
	import starling.animation.Juggler;
	import starling.core.Starling;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.*;
	import starling.textures.*;
	import starling.extensions.*;
	import flash.geom.Point;
	import flash.display.Graphics;
	
	public class MaParticule extends Sprite
	{
		
		private var nbrePex : int ;
		
		//attrributes
		// embed configuration XML
		[Embed(source="particle.pex", mimeType="application/octet-stream")]
		private static const FireConfig:Class;
		
	
		[Embed(source="particle3.pex", mimeType="application/octet-stream")]
		private static const FireConfig3:Class;
				
		[Embed(source="particle5.pex", mimeType="application/octet-stream")]
		private static const FireConfig5:Class;
	
		[Embed(source="particle8.pex", mimeType="application/octet-stream")]
		private static const FireConfig8:Class;
		
		// embed particle texture
		[Embed(source = "texture2.png")]
		private static const FireParticle2:Class;
		[Embed(source = "texture3.png")]
		private static const FireParticle3:Class;
		[Embed(source = "texture4.png")]
		private static const FireParticle4:Class;
		[Embed(source = "texture8.png")]
		private static const FireParticle8:Class;
		[Embed(source = "texture9.png")]
		private static const FireParticle9:Class;
		[Embed(source = "texture12.png")]
		private static const FireParticle12:Class;
		[Embed(source = "texture14.png")]
		private static const FireParticle14:Class;
		[Embed(source = "texture16.png")]
		private static const FireParticle16:Class;
		[Embed(source = "texture17.png")]
		private static const FireParticle17:Class;
		[Embed(source = "texture18.png")]
		private static const FireParticle18:Class;

	
		
		//ss
		// instantiate embedded objects
		public var psConf:XML;
		public var psConfig:XML = XML(new FireConfig());
		//public var psConfig1:XML = XML(new FireConfig1());
		//public var psConfig2:XML = XML(new FireConfig2());
		public var psConfig3:XML = XML(new FireConfig3());
		//public var psConfig4:XML = XML(new FireConfig4());
		public var psConfig5:XML = XML(new FireConfig5());
		public var psConfig8:XML = XML(new FireConfig8());
		
		public var psTexture2:Texture = Texture.fromBitmap(new FireParticle2());
		public var psTexture3:Texture = Texture.fromBitmap(new FireParticle3());
		public var psTexture4:Texture = Texture.fromBitmap(new FireParticle4());
		public var psTexture8:Texture = Texture.fromBitmap(new FireParticle8());
		public var psTexture9:Texture = Texture.fromBitmap(new FireParticle9());
		public var psTexture12:Texture = Texture.fromBitmap(new FireParticle12());
		public var psTexture14:Texture = Texture.fromBitmap(new FireParticle14());
		public var psTexture16:Texture = Texture.fromBitmap(new FireParticle16());
		public var psTexture17:Texture = Texture.fromBitmap(new FireParticle17());
		public var psTexture18:Texture = Texture.fromBitmap(new FireParticle18());

				
		// create particle system
		public var ps:PDParticleSystem;
		
		//juggler
		public var jg:Juggler;
 
		//hit box to detect collisions
		public var _hitbox:Quad;
		
		//test ligne
		private var _point2 : Graphics;
		
		//constructor
		public function MaParticule():void
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, added);
		}
		
		public function added(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, added);
			
			draw();
			
			
		}
		
		public function draw():void
		{
			
			trace ("Debut fonction draw............");
			jg = new Juggler();
			nbrePex = 2; //on commence par 0 
			
		
		
			var p : Number;
			p = Number(Math.round(Math.random() * nbrePex ));
						
			switch(p)
			{
				case 0:
				{
					ps = new PDParticleSystem(psConfig, psTexture12);
					//ps = new PDParticleSystem(psConfig, psTexture8); //double clic et +
					//ps = new PDParticleSystem(psConfig, psTexture17);   // touche long
					break;
				}
				case 1:
				{	ps = new PDParticleSystem(psConfig3, psTexture14);
				//	ps = new PDParticleSystem(psConfig3, psTexture9);  //double clic et +
				//	ps = new PDParticleSystem(psConfig3, psTexture18);   // touche long
					break;
				}
				case 2:
				{
					ps = new PDParticleSystem(psConfig5, psTexture3);
				//	ps = new PDParticleSystem(psConfig5, psTexture16);  //double clic et +
				//	ps = new PDParticleSystem(psConfig5, psTexture12);    // touche long
					break;
				}
				
				default :
				{	//ps = new PDParticleSystem(psConfig, psTexture18);
					ps = new PDParticleSystem(psConfig8, psTexture9);
				}
							
			}
			
			//draw Hit box
			_hitbox = new Quad(80, 80, 0x0000AA, true);
			_hitbox.alpha = 0;
			this.addChild(_hitbox);
				
			//draw particle system
			ps.x = _hitbox.x+_hitbox.width/2;
			ps.y =  _hitbox.y+_hitbox.width/2;
			
			// change position where particles are emitted
			ps.emitterX = _hitbox.x+_hitbox.width/2;
			ps.emitterY = _hitbox.y+_hitbox.width/2;
 
			// start emitting particles
			ps.start();
			addChild(ps);
			//jg.add(ps);
			Starling.juggler.add(ps);
			trace("particule ajoutee");
			
			
			//ecouteur drag n drop
			_hitbox.addEventListener(TouchEvent.TOUCH, touchHandler);
		}
		
		public function touchHandler(evt:TouchEvent):void
		{
			var touche:Touch = evt.getTouch(this);
			
			var target:Quad = evt.target as Quad;
			/*if (evt.getTouch(this, TouchPhase.BEGAN))
			{
				//trace(evt.getTouch(this).globalX);
				//trace(evt.getTouch(this).globalY);
			}*/
			if (touche != null)
			{
				if (touche.phase == TouchPhase.MOVED)
				{
					var position:Point = touche.getLocation(stage);
					//_par.ps.emitterX = touche.globalX;
					//trace("coucou");
					//_par.ps.emitterY = touche.globalY;
					_hitbox.x = position.x - target.width/2;
					_hitbox.y = position.y - target.height / 2;
					
					//bouger la particule
					ps.emitterX = _hitbox.x ;// + target.width / 2;
					ps.emitterY = _hitbox.y ;
				}
				
				if (touche.phase == TouchPhase.ENDED)
				{
					return;
				}
			}
			else
			{
				return;
			}
		}
	}
}